<!doctype html>
<html lang="en">
	<head>
		<title>three.js webgl - io - OBJ converter</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<style>
			body {
				background:#000;
				padding:0;
				margin:0;
				overflow:hidden;
				font-family:georgia;
				text-align:center;
				color: #ccc;
			}
			h1 {color: #ccc; }
			a { color:skyblue }
			canvas { pointer-events:none; z-index:10; }
		</style>
	</head>

	<body>
		<div id="d">
			<h1>Tron disk demo</h1>
			<p>Model by <a href="http://bkcore.com">Thibaut Despoulain</a>.
		</div>

		<script src="js/Three.js"></script>

		<script src="js/Detector.js"></script>
		<script src="js/Stats.js"></script>

		<script>

			var SCREEN_WIDTH = window.innerWidth;
			var SCREEN_HEIGHT = window.innerHeight;

			var container,stats;

			var camera, scene;
			var canvasRenderer, webglRenderer;

			var mesh, zmesh, geometry, pointLight, pmesh;

			var mouseX = 0, mouseY = 0;

			var windowHalfX = window.innerWidth / 2;
			var windowHalfY = window.innerHeight / 2;

			var render_canvas = 1, render_gl = 1;
			var has_gl = 0;

			document.addEventListener( 'mousemove', onDocumentMouseMove, false );

			init();
			animate();

			function init() {

				container = document.createElement( 'div' );
				document.body.appendChild( container );

				camera = new THREE.PerspectiveCamera( 75, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 100000 );
				camera.position.z = 140;

				scene = new THREE.Scene();

			

				// LIGHTS
				

				var ambient = new THREE.AmbientLight( 0xffffff );
				scene.add( ambient );

				pointLight = new THREE.PointLight( 0xffffff );
				pointLight.position.set( 0, 100, 0 );
				scene.add( pointLight );

				// RENDERER
					try {

						webglRenderer = new THREE.WebGLRenderer({
							antialias: true
						});
						webglRenderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
						webglRenderer.domElement.style.position = "relative";

						container.appendChild( webglRenderer.domElement );

						has_gl = 1;

					}
					catch (e) {
					}

				// STATS

				stats = new Stats();
				stats.domElement.style.position = 'absolute';
				stats.domElement.style.top = '0px';
				stats.domElement.style.zIndex = 100;
				container.appendChild( stats.domElement );

				// LOADER

				var loader = new THREE.JSONLoader(),
					callbackObj = function( geometry ) { createScene( geometry, 0, 0, 0, 0 ) };
				loader.load( "tron/trondisk.js", callbackObj );

				// POINT LIGHT REP

				var plight = new THREE.SphereGeometry( 5, 16, 8 ),
					plightmat = new THREE.MeshBasicMaterial( { color: 0x0099ff } );

				pmesh = new THREE.Mesh( plight, plightmat );

				pmesh.position = pointLight.position;

				pmesh.overdraw = true;

				scene.add( pmesh );

			}

			function createScene( geometry, x, y, z, b ) {

				zmesh = new THREE.Mesh( geometry, new THREE.MeshFaceMaterial() );
				zmesh.position.set( x, y, z );
				zmesh.scale.set( 3, 3, 3 );
				zmesh.overdraw = true;
				scene.add( zmesh );
			}

			function onDocumentMouseMove(event) {

				mouseX = ( event.clientX - windowHalfX );
				mouseY = ( event.clientY - windowHalfY );

			}

			//

			function animate() {

				requestAnimationFrame( animate );

				render();
				stats.update();

			}

			var t = 0.0;

			function render() {
				t += .01;
				camera.position.x += ( mouseX/2 - camera.position.x ) * .05;
				camera.position.y += ( - mouseY/2 - camera.position.y ) * .05;

				camera.lookAt( scene.position );
				pointLight.position.set( 0, Math.cos(t)*200, 0 );
				pmesh.position = pointLight.position;

				if ( render_gl && has_gl ) webglRenderer.render( scene, camera );

			}
		</script>

	</body>
</html>
